﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ninject;
using Mabalacat.CPS.BusinessLogic.Services.Interfaces;
using Web.Codes;
using Mabalacat.CPS.DataAccess.DataObjects;

namespace Web.Admin
{

    public partial class DivisionEntry : System.Web.UI.Page
    {
        [Inject]
        public IDivisionService _divisionService { get; set; }

        public bool IsSuccess { get; set; }
        public string NotifyText { get; set; }

        #region Implementations
        void BindGrid()
        {
            repeater.DataSource = _divisionService.GetAll();
            repeater.DataBind();
        }

        void Delete(int id)
        {
            _divisionService.Delete(id);
            IsSuccess = _divisionService.Save();
            NotifyText = "Successfully Deleted.";
            BindGrid();
        }

        void Edit(int id, RepeaterItem item)
        {            
            var codeTxt = ((Literal)item.FindControl("CodeTxt")).Text;
            var nameTxt = ((Literal)item.FindControl("NameTxt")).Text;
            var signatoryTxt = ((Literal)item.FindControl("SignatoryTxt")).Text;

            DivCodeTxt.Text = codeTxt;
            DivisionId.Value = id.ToString();
            DivNameTxt.Text = codeTxt;
            DivSignatoryTxt.Text = codeTxt;
            SubmitBtn.Text = "Update";
        }

        void Update(int id)
        {
            var division = _divisionService.GetById(id);
            if (division == null)
            {
                Add();
                return;
            }
            DataToObject(ref division);

            _divisionService.Update(division);
            IsSuccess = _divisionService.Save();
            NotifyText = "Successfully Updated Record.";
            ClearForm();
            BindGrid();            
        }
        void Add()
        {
            var division = new Division();

            DataToObject(ref division);

            _divisionService.Add(division);
            IsSuccess = _divisionService.Save();
            NotifyText = "Successfully Added New Record.";
            ClearForm();
            BindGrid();
        }
        void ClearForm()
        {
            DivCodeTxt.Text = string.Empty;
            DivisionId.Value = "0";
            DivNameTxt.Text = string.Empty;
            DivSignatoryTxt.Text = string.Empty;
            SubmitBtn.Text = "Submit";
        }
        void Save()
        {
            int divId;
            int.TryParse(DivisionId.Value, out divId);
            if (divId == 0)
                Add();
            else
                Update(divId);
        }
        void DataToObject(ref Division div)
        {
            div.Code = DivCodeTxt.Text;
            div.Name = DivNameTxt.Text;
            div.Signatory = DivSignatoryTxt.Text;
        }
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
                BindGrid();
        }

        protected void repeater_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            int intTemp = 0;
            if (int.TryParse((string)e.CommandArgument, out intTemp))
            {
                switch (e.CommandName.ToLower())
                {
                    case Constants.Update_Command:
                        Edit(intTemp, e.Item);
                        break;
                    case Constants.Delete_Command:
                        Delete(intTemp);
                        break;
                }
            }
        }

        protected void CancelBtn_Click(object sender, EventArgs e)
        {
            IsSuccess = false;
            ClearForm();            
        }

        protected void SubmitBtn_Click(object sender, EventArgs e)
        {
            Save();
        }

    }
}